<template>
    <el-row class="lives-news">
        <el-row>
            <el-col class="title">
                <h3>直播时段</h3>
            </el-col>
        </el-row>
        <el-row class="search-row">
            <el-form class="inline-form" :inline="true">
                <el-form-item>
                    <el-button type="primary" @click="showAddDialog"><i class="el-icon-plus el-icon--left"></i>添加时段</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-col>
                <el-table :data="LTList">
                    <el-table-column prop="liveTimeId" label="ID" width="220px" align="center"></el-table-column>
                    <el-table-column prop="name" label="时段名称" width="200px" align="center"></el-table-column>
                    <el-table-column prop="timeStart" label="开始时间" align="center"></el-table-column>
                    <el-table-column  prop="timeEnd"label="结束时间" align="center"></el-table-column>
                    <el-table-column label="操作" width="240px" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" size="small" @click="updateCustom(scope.$index, scope.row)">修改</el-button>
                            <el-button type="danger" size="small" @click="deleteCustom(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-dialog :title="(isAdd ? '新增':'修改') + '时段'" :visible.sync="addDialog" size="tiny" @close="cancelAddCategory" class="dialog">
                <el-form :model="liveTime" label-width="120px">
                    <el-form-item label="时段名称">
                        <el-input v-model="liveTime.name" auto-complete="on" placeholder="请输入时间段名称"></el-input>
                    </el-form-item>
                    <el-form-item label="开始时间">
                        <el-time-picker v-model="liveTime.timeStart" placeholder="开始时间段"></el-time-picker>
                    </el-form-item>
                    <el-form-item label="结束时间">
                        <el-time-picker v-model="liveTime.timeEnd" placeholder="结束时间段"></el-time-picker>
                    </el-form-item>
                </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAddCategory">取 消</el-button>
                <el-button type="primary" @click="addCategory" :loading="addLoading">保 存</el-button>
            </div>
        </el-dialog>
    </el-row>
</template>
<script>
import {
    getLiveTime,
    addLiveTime,
    updateLiveTime,
    deleteLiveTime
} from '@/api/api'
import {exportUser} from '@/api/excel'
export default {
    data: function() {
        return {
            LTList: [],
            addDialog: false,
            liveTime: {
                liveTimeId: null,
                name: '',
                timeStart: '',
                timeEnd: ''
            },
            addLoading: false,
            isAdd: true
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getLT();
    },
    methods: {
        //获取视频分类列表
        getLT: function() {
            getLiveTime().then(data => {
                console.log(data)
                this.LTList = data;
            })
        },
        showAddDialog: function(){
            this.addDialog = true;
            this.isAdd = true;
        },
        cancelAddCategory: function(){
            this.addDialog = false;
            this.liveTime = {
                liveTimeId: null,
                name: '',
                timeStart: '',
                timeEnd: ''
            }
        },
        addCategory: function(){
            console.log(this.liveTime)
            if(!this.liveTime.name){
                this.$message({
                    type: 'error',
                    message: '请输入时间段名称!'
                });
                return;
            }
            if(!this.liveTime.timeStart){
                this.$message({
                    type: 'error',
                    message: '请选择开始时间段!'
                });
                return;
            }
            if(!this.liveTime.timeEnd){
                this.$message({
                    type: 'error',
                    message: '请选择结束时间段!'
                });
                return;
            }
            var timeStart = this.moment(this.liveTime.timeStart).format('HH:mm:ss');
            var timeEnd = this.moment(this.liveTime.timeEnd).format('HH:mm:ss');
            this.liveTime.timeStart = timeStart;
            this.liveTime.timeEnd = timeEnd;
            if(this.isAdd){
                addLiveTime(this.liveTime).then(res => {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                    this.getLT();
                    this.cancelAddCategory()
                })
            }else{
                updateLiveTime(this.liveTime).then(res => {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                    this.getLT();
                    this.cancelAddCategory()
                })
            }
        },
        updateCustom: function(index, row){
           this.addDialog = true;
           this.isAdd = false;
           this.liveTime = {
                liveTimeId: row.liveTimeId,
                name: row.name,
                timeStart: new Date('2018-01-01 ' + row.timeStart),
                timeEnd: new Date('2018-01-01 ' + row.timeEnd)
           }
        },
        deleteCustom: function(index, row){
            this.$confirm('确定要删除该时间段?', '删除时段', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteLiveTime(row.liveTimeId).then(() => {
                    this.getLT();
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        }
    }
}
</script>
<style scoped lang="scss">
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
}
.search-input{
	width: 200px;
}
</style>